import axios from 'axios';

const globalConfig = require('../../globalConfig');


export let apiPath = globalConfig.servers.apiPath;
export let exSystems = globalConfig.servers.exSystems;
export let GLobal_SystemId = globalConfig.servers.systemId;
export const requestLogin = params => {
	return axios.post(`${apiPath}/sys/auth/info/login`, params).then(res => res.data);
};

export const AjaxByGet = (url, param) => {
	return axios({
		method: 'get',
		url: url.indexOf('http') > -1 ? `${url}` : `${apiPath}/${url}`,
		params: param
	}).then(res => res.data);
};
export const AjaxByDelete = (url, param) => {
	return axios({
		method: 'delete',
		url: url.indexOf('http') > -1 ? `${url}` : `${apiPath}/${url}`,
		params: param
	}).then(res => res.data);
};
export const AjaxByPost = (url, param) => {
	return axios({
		method: 'post',
		url: url.indexOf('http') > -1 ? `${url}` : `${apiPath}/${url}`,
		data: param
	}).then(res => res.data);
};
export const AjaxByPut = (url, param) => {
	return axios({
		method: 'put',
		url: url.indexOf('http') > -1 ? `${url}` : `${apiPath}/${url}`,
		data: param
	}).then(res => res.data);
};
export const AjaxByAll = (method,url, param) => {
	var form={
		method: method,
		url: url.indexOf('http') > -1 ? `${url}` : `${apiPath}/${url}`,
	}
	if(method==='post'||method==='put'){
		form['data']=param;
	}else if(method==='get'||method==='delete'){
		form['params']=param;
	}else{
		form['data']=param;
	}
	return axios(form).then(res => res.data);
};

export const AjaxDownloadFile = (url, param,fileName) => {
    return axios({
        method: 'get',
        url: url.indexOf('http') > -1 ? `${url}` : `${apiPath}/${url}`,
        data: param,
        responseType: 'blob'
    }).then(data => {
        {
            if (!data) {
                return
            }

            let url = window.URL.createObjectURL(new Blob([data.data], {type: "application/octet-binary"}))
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            link.setAttribute('download', fileName)

            document.body.appendChild(link)
            link.click()
        }

	});
};



/**
 *获取唯一的ID
 */
export const  GetUid=()=> {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
};


export  const JumpUrl=(url,router)=>{
	let jumpUrl=window.location.href;
	let preUrl=getPreUrl(jumpUrl);
	if(preUrl===getPreUrl(url)){
        router.push(url.replace(preUrl,''));
	} else {
		window.open(url,"_blank");
	}
}

function  getPreUrl(url){
    let  index = url.lastIndexOf("#");
	return   url.substring(0,index+1);
}
